package yf.mall.dysdk.model.request.video;

import com.alibaba.fastjson.JSONObject;
import com.yf.response.RRException;
import com.yf.utils.HttpTookit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import yf.mall.utils.StringUtil;
import yf.mall.vo.app.dy.DyVideoVO;

import java.util.HashMap;
import java.util.Map;

/**
 * 淘宝买的海绵去水印 功夫去水印 可能需要换token
 * token特别容易过期 暂时废弃
 * https://qsy.ququba.top/index.php
 *
 *
 */
@Slf4j
public class DyVideoRequest4 extends DyVideoBaseRequest {


    public DyVideoRequest4() {
        this.url = "https://analysis.20kaka.cn/api/wechat_v2/analysis";
        this.method = HttpMethod.POST;
        this.token = "87v0KfRkXUM6D5OwP524mySEz4ah/N1SedSizVORol9GCnctDI3U1dTSW2tSR4G1ZLiwTtZk+54Zp3av05xTfLtvbqWbjpFjKPpZJD6cHu3KdeG9aCYDSuzEXgYJZfPUwtwA+gXGseIsM224vbYWuw==";
        Map headers = new HashMap();
        headers.put("Host","analysis.20kaka.cn");
        headers.put("referer","https://servicewechat.com/wxfff340c452b6fc81/43/page-frame.html");
        this.headers = headers;
    }


    @Override
    public DyVideoVO parseVideo(String shareVideo) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",shareVideo);
        jsonObject.put("token",token);
        Long current = System.currentTimeMillis();
        String res = HttpTookit.doPostStr(url, jsonObject.toJSONString(),headers);
        current = System.currentTimeMillis() - current;
        log.info("请求去水印接口,url:{},res:{}", url, res);
        if (StringUtil.isBlank(res)) {
            log.error("请求去水印接口失败,url:{},res:{}", url, res);
            throw new RRException("去水印请求失败!");
        }
         jsonObject = JSONObject.parseObject(res);
        if (!jsonObject.get("code").equals(101) || !jsonObject.containsKey("data")) {
            log.error("请求去水印接口失败,url:{},res:{}", url, res);
            throw new RRException("去水印请求失败!");
        }
        JSONObject data = jsonObject.getJSONObject("data");
        if (!data.containsKey("url")) {
            log.error("请求去水印接口失败,url:{},res:{}", url, res);
        }
        return new DyVideoVO(data.getString("url"), null, data.getString("cover"), data.getString("title"),current);
    }
}
